package com.farm.kaiping.controller;

import com.farm.common.security.AnonymousAccess;
import com.farm.common.security.SecurityUtils;
import com.farm.common.util.JwtTokenUtil;
import com.farm.common.util.R;
import com.farm.kaiping.entity.FarmUser;
import com.farm.kaiping.po.SysRegionPO;
import com.farm.kaiping.service.SysRegionService;
import com.farm.kaiping.service.SysUserService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/test")
@AllArgsConstructor
public class TestController {

    private final SysUserService sysUserService;
    private final JwtTokenUtil jwtTokenUtil;
    private final SysRegionService sysRegionService;

    @ApiOperation(value = "需登录/token解析/权限", notes = "权限：view_test_checkAuth")
    @PreAuthorize("@pms.hasPermission('ROLE_1','view_test_checkAuth','zhuhui')")
    @PostMapping("/checkAuth")
    public R checkPermission(HttpServletRequest request) {
        System.out.println("================================3");
        System.out.println("需登录和权限: -->"+SecurityUtils.getUser().getUsername());
        return R.ok("需登录/token解析/权限: "+SecurityUtils.getUser().getUsername());
    }

    @ApiOperation(value = "需登录/token解析/没有权限", notes = "权限：view_test_page")
    @PreAuthorize("@pms.hasPermission('ROLE_1','view_test_checkNoAuth','zhuhui')")
    @PostMapping("/checkNoAuth")
    public R checkPermissionNoAuth(HttpServletRequest request) {
        System.out.println("================================4");
        System.out.println("需登录和没有权限: -->"+SecurityUtils.getUser().getUsername());
        return R.ok("需登录/token解析/没有权限: "+SecurityUtils.getUser().getUsername());
    }

    @ApiOperation(value = "仅需登录即可", notes = "权限：没有权限，仅需登录即可")
    @PreAuthorize("@pms.hasPermission('ROLE_ADMIN','VIEW_LOGIN')")
    @PostMapping("/checkLogin")
    public R checkLogin(HttpServletRequest request) {
        System.out.println("================================2");
        System.out.println("仅需登录即可: -->"+SecurityUtils.getUser().getUsername());
        return R.ok("仅需登录即可: "+SecurityUtils.getUser().getUsername());
    }

    @ApiOperation(value = "测试", notes = "权限：无需登录，不需要权限")
    @AnonymousAccess
    @GetMapping("/hello/{userId}")
    public R hello(@PathVariable("userId") Integer userId) {
        System.out.println("================================1");
        System.out.println("无需登录，不需要权限");
        FarmUser cacheSysUserByUserId = sysUserService.getCacheSysUserByUserId(userId);
        return R.ok(cacheSysUserByUserId,"无需登录，不需要权限");
    }

    @ApiOperation(value = "生成Token", notes = "权限：无需登录，不需要权限")
    @AnonymousAccess
    @GetMapping("/token/{userId}")
    public R token(@PathVariable("userId") Integer userId) {
        String token = jwtTokenUtil.generateToken(userId);
        return R.ok(token,"生成Token");
    }

    @ApiOperation(value = "根据ID获取行政区划缓存", notes = "权限：无需登录，不需要权限")
    @AnonymousAccess
    @GetMapping("/region/{id}")
    public R getCacheAreaNameById(@PathVariable("id") Integer id) {
        // SysRegion cacheAreaNameById = sysRegionService.getCacheAreaNameById(id);
        // return R.ok(cacheAreaNameById,"根据ID获取行政区划缓存");
        return R.ok("根据ID获取行政区划缓存");
    }

    @ApiOperation(value = "根据ID获取行政区划缓存", notes = "权限：无需登录，不需要权限")
    @AnonymousAccess
    @PostMapping("/regionCache")
    public R getCacheAreaNameById(@RequestBody SysRegionPO sysAreaPO) {
        List<Integer> idList = sysAreaPO.getIdList();
        // Map<Integer, SysRegion> cacheAreaNameByIds = sysRegionService.getCacheAreaNameByIds(sysAreaPO.getIdList());
        // return R.ok(cacheAreaNameByIds,"根据ID获取行政区划缓存");
        return R.ok("根据ID获取行政区划缓存");
    }


}
